** Regressions

log using "$project_path/output/appendix_tables/Appendix_Table_D3.smcl", replace

use "$project_path/data/4_regdata/regdata.dta", clear

local variables = "infant_mr hdi hdi_without_income lifeexp_index educ_index income_index life_exp crime hospital_beds undernourishment gini_wb gini_inc_wid gini_wea_wid poverty_gap poverty_head_190 in_war interstate_war intrastate_war extrastate_war mid_level any_coup in_conflict intra_conflict stability_viol"
local negative_variables = "infant_mr crime undernourishment gini_wb gini_inc_wid gini_wea_wid poverty_gap poverty_head_190 in_war interstate_war intrastate_war extrastate_war mid_level any_coup in_conflict intra_conflict"
foreach v in `negative_variables' {
	cap gen mstd_diff_m1_`v' = -std_diff_m1_`v'
}

foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
	}
}

log close

** Building .tex table

//Generate a temporary dta file with variables description. We use an id variable that follows the order of the local `variables'.
preserve
gen description = ""

local i = 1
foreach v in `variables'{
	
	if "`v'" == "infant_mr" local description = "(Minus) Infant mortality rate"
	if "`v'" == "hdi" local description = "HDI"
	if "`v'" == "hdi_without_income" local description = "HDI (without income component)"
	if "`v'" == "lifeexp_index" local description = "HDI: Life expectancy component"
	if "`v'" == "educ_index" local description = "HDI: Education component"
	if "`v'" == "income_index" local description = "HDI: Income component"
	if "`v'" == "life_exp" local description = "Life expectancy"
	if "`v'" == "crime" local description = "(Minus) Homicide rate"
	if "`v'" == "hospital_beds" local description = "Hospital beds per capita"
	if "`v'" == "undernourishment" local description = "(Minus) Undernourishment (\% of pop.)"
	if "`v'" == "gini_wb" local description = "(Minus) Gini index"
	if "`v'" == "gini_inc_wid" local description = "(Minus) Gini index, income"
	if "`v'" == "gini_wea_wid" local description = "(Minus) Gini index, wealth"
	if "`v'" == "poverty_gap" local description = "(Minus) Poverty gap at 1.90 USD/day (\%)"
	if "`v'" == "poverty_head_190" local description = "(Minus) Poverty (\% of population under 1.90 USD/day)"
	if "`v'" == "in_war" local description = "(Minus) In war"
	if "`v'" == "interstate_war" local description = "(Minus) In interstate war"
	if "`v'" == "intrastate_war" local description = "(Minus) In intrastate war"
	if "`v'" == "extrastate_war" local description = "(Minus) In extra-state war"
	if "`v'" == "mid_level" local description = "(Minus) Conflict intensity level"
	if "`v'" == "any_coup" local description = "(Minus) Any coup"
	if "`v'" == "in_conflict" local description = "(Minus) Involved in a conflict"
	if "`v'" == "intra_conflict" local description = "(Minus) Intra-state conflict"
	if "`v'" == "stability_viol" local description = "Political stability"
	
	qui replace description = "`description'" in `i'
	
	local ++i
}
keep description 
drop if description == ""
gen id = _n
tempfile description_dta
save `description_dta'
restore

//Generate a temporary dta file with source variable
preserve
gen source = ""
local i = 1
foreach v in `variables' {
	
	replace source = "``v'[note1]'" in `i'
	local ++i
	
}	
keep source
drop if source == ""
gen id = _n
replace source = substr(source, 9,.)
tempfile source_dta
save `source_dta'
restore

//Prepare matrix of coefficients and other statistics from the regression
local rows : word count `variables'
local nrows `rows'
local ncols 4
mat Coefs = J(`nrows', `ncols', .)


local i = 1
foreach v in `variables' {
	if strpos("`negative_variables'", "`v'"){
		rdrobust mstd_diff_m1_`v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i
		
	}
	else {
		rdrobust std_diff_m1_`v' runvar, all
		
		//Store estimates and stats
		local b_`i': di %5.3f e(tau_cl)
		local se_`i': di %5.3f e(se_tau_rb)
		local p_`i': di %5.3f e(pv_rb)
		loca N_`i': di %6.0f e(N)
		
		//Fill the matrix
		mat Coefs[`i',1] = `b_`i''
		mat Coefs[`i',2] = `se_`i''
		mat Coefs[`i',3] = `p_`i''	
		mat Coefs[`i',4] = `N_`i''
		
		local ++i

	}
}

mat list Coefs

********************************************************************************
* 								EXPORT TABLE
********************************************************************************			
// Clear the dataset, and use the matrix as dataset			

clear
svmat Coefs

rename Coefs1 estimates
rename Coefs2 std_err
rename Coefs3 pval
rename Coefs4 N

gen outcome = ""

gen id = _n
order id outcome, first

tostring estimates, replace force format(%04.3f)

//Add stars
local 3_star 0.01
local 2_star 0.05
local 1_star 0.10

replace estimates = estimates + "***" if pval < `3_star'
replace estimates = estimates + "**" if  pval < `2_star' & pval > `3_star'
replace estimates = estimates + "*" if   pval < `1_star' & pval > `2_star'

foreach var in std_err pval{
	
	tostring `var', replace force format(%04.3f)
	
}

replace std_err = "(" + std_err + ")"
replace pval = "[" + pval + "]"

//Attach source variable and description
merge 1:1 id using `source_dta', assert(match) nogen
merge 1:1 id using `description_dta', assert(match) nogen

replace outcome = description

drop id description


label var outcome ""
label var estimates "Est."
label var std_err "SE"
label var pval "p-val"
label var N "N"
label var source "Source"


//Prepare texfile
local title "Effects on additional human development variables"
local label_key "tab:social_variables_baseline"
local output "$project_path/output/appendix_tables/Appendix_Table_D3.tex"
local footnote = "\emph{Notes}: This table reports RD estimates corresponding to equation (\ref{eq:baseline_rdd}) for measures of human development (including measures of peace), expressed in standard deviation terms."

texsave using "`output'", replace varlabels frag nofix 	location(H) ///
					      align(p{6.8cm} p{1.7cm} p{1.5cm} p{1.5cm} p{1.3cm} p{3.2cm}) ///
						  title("`title'")  ///
						  footnote("`footnote'") ///
						  marker("`label_key'") 
